package org.example.v5.statemachine;

import java.util.Map;
import java.util.function.Supplier;

/**
 * StateFactory 类用于创建状态实例。
 * @param <S> 状态类型
 * @param <E> 事件类型
 */
public class StateFactory<S, E extends Enum<E>> {
    private final Map<S, Supplier<StateAction<StateMachine<S, E>, E>>> stateMap;

    public StateFactory(Map<S, Supplier<StateAction<StateMachine<S, E>, E>>> stateMap) {
        this.stateMap = stateMap;
    }

    public StateAction<StateMachine<S, E>, E> getState(S state) {
        Supplier<StateAction<StateMachine<S, E>, E>> supplier = stateMap.get(state);
        if (supplier != null) {
            return supplier.get();
        }
        throw new IllegalArgumentException("未知的状态: " + state);
    }
}
